package com.hzb;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.joda.time.DateTime;
import org.junit.Test;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

/**
 * 功能描述
 *
 * @author 和振斌
 * @date 2020/7/31
 * @return
 */
public class ExcelWriteTest {

    String PATH="F:\\springBoot_SSM\\springboot文件上传下载\\excel_poi\\src\\main\\resources\\file";
    @Test
    public  void testWrite03() throws Exception {
        //1.创建工作簿
        Workbook workbook=new HSSFWorkbook();
        //2.创建工作表
        Sheet sheet =workbook.createSheet("和振斌");
        //3.创建行,0代表第一行(1,1)
        Row row=sheet.createRow(0);
        Cell cell11=row.createCell(0);
        cell11.setCellValue("测试1");
        //4.创建一个单元格(1,2)
        Cell cell = row.createCell(1);
        cell.setCellValue("测试2");

        //第二行(2,1)
        Row row2=sheet.createRow(1);
        Cell cell21 = row2.createCell(0);
        cell21.setCellValue("测试3");
        //第二行(2,2)
        Cell cell22=row2.createCell(1);
        String s = new DateTime().toString("yyyy-MM-dd HH:mm:ss");
        cell22.setCellValue(s);

        //生成一张表03版本就是使用xls结尾
        FileOutputStream fileOutputStream = new FileOutputStream(PATH + "测试表.xls");
        //输出
        workbook.write(fileOutputStream);
        //关闭流
        fileOutputStream.close();
        System.out.println("生成表成功！");
    }

    @Test
    public  void testWrite07() throws Exception {
        //1.创建工作簿07
        Workbook workbook=new HSSFWorkbook();
        //2.创建工作表
        Sheet sheet =workbook.createSheet("和振斌");
        //3.创建行,0代表第一行(1,1)
        Row row=sheet.createRow(0);
        Cell cell11=row.createCell(0);
        cell11.setCellValue("测试1");
        //4.创建一个单元格(1,2)
        Cell cell = row.createCell(1);
        cell.setCellValue("测试2");

        //第二行(2,1)
        Row row2=sheet.createRow(1);
        Cell cell21 = row2.createCell(0);
        cell21.setCellValue("测试3");
        //第二行(2,2)
        Cell cell22=row2.createCell(1);
        String s = new DateTime().toString("yyyy-MM-dd HH:mm:ss");
        cell22.setCellValue(s);

        //生成一张表03版本就是使用xlsx结尾
        FileOutputStream fileOutputStream = new FileOutputStream(PATH + "测试表07.xlsx");
        //输出
        workbook.write(fileOutputStream);
        //关闭流
        fileOutputStream.close();
        System.out.println("07生成表成功！");
    }
    @Test
    public void testWrite03BigDate() throws Exception {
        //时间
        long begin = System.currentTimeMillis();
        //创建簿
        Workbook workbook=new HSSFWorkbook();
        //创建表
        Sheet sheet = workbook.createSheet();
        //写入数据
        for(int rowNum=0;rowNum< 65536; rowNum++){
            Row row = sheet.createRow(rowNum);
            for(int cellNum= 0;cellNum< 10;cellNum++){
                Cell cell=row.createCell(cellNum);
                cell.setCellValue(cellNum);
            }
        }
        System.out.println("over");
        FileOutputStream fileOutputStream = new FileOutputStream(PATH + "testWrite03BigDate.xls");
        workbook.write(fileOutputStream);
        fileOutputStream.close();
        //结束时间
        long end=System.currentTimeMillis();
       System.out.println((double)(end-begin)/1000);
    }

    @Test
    public void testWrite07BigDate() throws Exception {
        //时间
        long begin = System.currentTimeMillis();
        //创建簿
        Workbook workbook=new XSSFWorkbook();
        //创建表
        Sheet sheet = workbook.createSheet();
        //写入数据
        for(int rowNum=0;rowNum< 100000; rowNum++){
            Row row = sheet.createRow(rowNum);
            for(int cellNum= 0;cellNum< 10;cellNum++){
                Cell cell=row.createCell(cellNum);
                cell.setCellValue(cellNum);
            }
        }
        System.out.println("over");
        FileOutputStream fileOutputStream = new FileOutputStream(PATH + "testWrite07BigDate.xlsx");
        workbook.write(fileOutputStream);
        fileOutputStream.close();
        //结束时间
        long end=System.currentTimeMillis();
        System.out.println((double)(end-begin)/1000);
    }
}
